package com.group.service.impl;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.group.dao.TbBillLogMapper;
import com.group.dao.TbBillMapper;
import com.group.dao.TbContractMapper;
import com.group.pojo.Common;
import com.group.pojo.QCloudCosResult;
import com.group.pojo.TbBill;
import com.group.pojo.TbBillExample;
import com.group.pojo.TbBillExample.Criteria;
import com.group.pojo.TbBillLog;
import com.group.pojo.TbContract;
import com.group.pojo.TbCustomer;
import com.group.service.BillService;
import com.group.service.CommonService;
import com.group.util.CommonUtil;
import com.group.util.QCloudCosUtil;

@Service
public class BillServiceImpl implements BillService {
	@Autowired
	private TbBillMapper billMapper;
	@Autowired
	private TbBillLogMapper billLogMapper;
	@Autowired
	private TbContractMapper contractMapper;
	@Autowired
	private CommonService commonService;
	
	
	private void setCondition(Criteria criteria, TbBill bill) {
		String id = bill.getId();
		if (id!= null && !"".equals(id)) {
			criteria.andIdEqualTo(id);
		}
		
		String customerId = bill.getCustomerId();
		if (customerId != null && !"".equals(customerId)) {
			criteria.andCustomerIdEqualTo(customerId);
		}
		
		String billStatus = bill.getBillStatus();
		if (billStatus != null && !"".equals(billStatus)) {
			criteria.andBillStatusEqualTo(billStatus);
		}
		
		String contractId = bill.getContractId();
		if (contractId != null && !"".equals(contractId)) {
			criteria.andContractIdEqualTo(contractId);
		}
		
	}
	
	
	@Override
	public List<TbBill> query(TbBill bill) {
		TbBillExample example = new TbBillExample();
		
		setCondition(example.createCriteria(), bill);
		
		List<TbBill> list = billMapper.selectByExample(example);
		return list;
	}

	@Override
	public Map<String, String> save(HttpServletRequest request, TbBill bill, MultipartFile billImg) {
		//图片地址
		String cosPath = CommonUtil.getUploadImgUrl("contract-customer", billImg);
		byte[] bytes = null;
		try {
			bytes = billImg.getBytes();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		
		//保存图片
		String uploadFileResult = QCloudCosUtil.uploadFile(bytes, cosPath);
		
		QCloudCosResult parse = JSON.parseObject(uploadFileResult, QCloudCosResult.class);
		
		Integer code = parse.getCode();
		
		Map<String, String> returnMap = CommonUtil.getReturnMap("success", "false");
		if (code == 0) {
			String access_url = parse.getData().getAccess_url();
			//设置图片地址
			bill.setBillImageUrl(access_url);
			//设置id
			bill.setId(commonService.getTbCurrentDateNextId(new Common("tb_bill", CommonUtil.getCurrentDate("yyyyMMdd"))));

			TbCustomer cus = (TbCustomer) request.getSession().getAttribute("CUSTOMER_SESSION");
			bill.setCustomerId(cus.getId());
			bill.setCreateTime(new Date());
			
			//设置状态
			bill.setBillStatus("1");
			
			int insert = billMapper.insert(bill);
			if (insert == 1) {
				
				//bill日志
				TbBillLog log = new TbBillLog();
				log.setId(commonService.getTbCurrentDateNextId(new Common("tb_bill_log", CommonUtil.getCurrentDate("yyyyMMdd"))));
				log.setBillId(bill.getId());
				log.setOperateEvent("申请报修");
				
				log.setOperateUserId(cus.getId());
				log.setOperateUserType("1");//表示客户
				log.setOperateTime(new Date());
				int insert2 = billLogMapper.insert(log);
				if (insert2 == 1) {
					
					//合同更新
					TbContract contract = new TbContract();
					contract.setId(bill.getContractId());
					contract.setContractStatus("2");
					int update = contractMapper.updateByPrimaryKeySelective(contract);
					if (update == 1) {
						returnMap.put("success", "true");
					}
				}
			}
		}
		return returnMap;
	}

	/**
	 * 提交维修地址：
	 * 报修单tb_bill表更新状态是 3，提交信息
	 * 报修日志表插入一条记录
	 */
	@Override
	public Map<String, String> submitAddress(HttpServletRequest request, TbBill bill) {
		
		Map<String, String> returnMap = CommonUtil.getReturnMap("success", "false");
		
		
		bill.setBillStatus("3");
		
		int update = billMapper.updateByPrimaryKeySelective(bill);
		if (update == 1) {
			//bill日志
			TbBillLog log = new TbBillLog();
			log.setId(commonService.getTbCurrentDateNextId(new Common("tb_bill_log", CommonUtil.getCurrentDate("yyyyMMdd"))));
			log.setBillId(bill.getId());
			log.setOperateEvent("提交维修地址");
			
			TbCustomer cus = (TbCustomer) request.getSession().getAttribute("CUSTOMER_SESSION");
			log.setOperateUserId(cus.getId());
			log.setOperateUserType("1");//表示客户
			log.setOperateTime(new Date());
			int insert  = billLogMapper.insert(log);
			if (insert == 1)  {
				returnMap.put("success", "true");
			}
		}
		return returnMap;
	}


	@Override
	public Map<String, String> update(TbBill bill, HttpServletRequest request) {
		Map<String, String> returnMap = CommonUtil.getReturnMap("success", "false");
		bill.setBillStatus("6");
		bill.setUpdateTime(new Date());
		int update = billMapper.updateByPrimaryKeySelective(bill);
		if (update == 1) {
			//bill日志
			TbBillLog log = new TbBillLog();
			log.setId(commonService.getTbCurrentDateNextId(new Common("tb_bill_log", CommonUtil.getCurrentDate("yyyyMMdd"))));
			log.setBillId(bill.getId());
			log.setOperateEvent("报修评价");
			
			TbCustomer cus = (TbCustomer) request.getSession().getAttribute("CUSTOMER_SESSION");
			log.setOperateUserId(cus.getId());
			log.setOperateUserType("1");//表示客户
			log.setOperateTime(new Date());
			int insert  = billLogMapper.insert(log);
			if (insert == 1)  {
				returnMap.put("success", "true");
			}
		}
		return returnMap;
	}

	
	



}
